package com.git.plane;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 主运行类
 * 
 * @author lichao
 *
 * @date 2016年3月14日
 */
public class ShootGame extends JPanel {
	/**
	 * 默认的序列号
	 */
	private static final long serialVersionUID = 1L;
	// 游戏界面属性
	public static final int WIDTH = 400;// 界面宽
	public static final int HEIGHT = 654;// 界面高
	public static BufferedImage background;// 背景图片
	public static BufferedImage start;// 开始界面
	public static BufferedImage airplane;// 飞机
	public static BufferedImage bee;// 蜜蜂
	public static BufferedImage bullet;// 子弹
	public static BufferedImage hero0;// 英雄图形0
	public static BufferedImage hero1;// 英雄图形1
	public static BufferedImage pause;// 暂停图
	public static BufferedImage gameover;// 游戏结束图

	private FlyingObject[] flyings = {};
	private Bullet[] bullets = {};
	private Hero hero = new Hero();

	public ShootGame() {
		flyings = new FlyingObject[2];// 初始化飞行物
		flyings[0] = new AirPlane();// 飞行物中一个为敌机
		flyings[1] = new Bee();// 一个为蜜蜂
		bullets = new Bullet[2];// 初始化一颗子弹
		bullets[0] = new Bullet(200, 350);// 子弹位置
		bullets[1] = new Bullet(250, 300);
	}

	/**
	 * 静态代码块，表示程序运行时就加载图片信息
	 */
	static {
		try {
			background = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("background.png"));
			start = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("start.png"));
			airplane = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("airplane.png"));
			bee = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("bee.png"));
			bullet = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("bullet.png"));
			hero0 = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("hero0.png"));
			hero1 = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("hero1.png"));
			pause = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("pause.png"));
			gameover = ImageIO.read(ShootGame.class.getClassLoader()
					.getResourceAsStream("gameover.png"));
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("加载图片信息出错");
		}
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(background, 0, 0, null);
		paintHero(g);
		paintBullets(g);
		paintFlyingObjects(g);
	}

	/**
	 * 画飞行物
	 * 
	 * @param g
	 *            画笔
	 */
	private void paintFlyingObjects(Graphics g) {
		for (int i = 0; i < flyings.length; i++) {
			FlyingObject fly = flyings[i];
			g.drawImage(fly.image, fly.x, fly.y, null);
		}
	}

	/**
	 * 画子弹
	 * 
	 * @param g
	 *            画笔
	 */
	private void paintBullets(Graphics g) {
		for (int i = 0; i < bullets.length; i++) {
			Bullet b = bullets[i];
			g.drawImage(b.image, b.x, b.y, null);
		}
	}

	/**
	 * 画英雄机
	 * 
	 * @param g
	 *            画笔
	 */
	private void paintHero(Graphics g) {
		g.drawImage(hero.image, hero.x, hero.y, null);
	}

	/**
	 * 随机产生下一个飞行物，是蜜蜂与飞机的概率为1：19,蜜蜂产生的概率小一些
	 * 
	 * @param args
	 */

	private static FlyingObject nextOne() {
		Random random = new Random();
		int type = random.nextInt(20);// [0,19)
		if (type == 0) {
			return new Bee();
		} else {
			return new AirPlane();
		}
	}

	int flyEnterIndex = 0;

	/**
	 * 记录飞机进场次数，随着时间的增加，飞行物的数目增多
	 */
	private void enterAction() {
		flyEnterIndex++;
		if (flyEnterIndex % 40 == 0) {
			FlyingObject obj = nextOne();// 随机生成新的飞行物
			flyings = Arrays.copyOf(flyings, flyings.length + 1);// 扩容
			flyings[flyings.length - 1] = obj;// 放到最后一位
		}
	}

	/**
	 * 物体移动的方法
	 */
	private void stepAction() {
		// 飞行物飞行方法
		for (int i = 0; i < flyings.length; i++) {
			flyings[i].step();
		}
		// 子弹飞行
		for (int i = 0; i < bullets.length; i++) {
			bullets[i].step();
		}
		// 英雄机飞行
		hero.step();
	}

	int shootIndex = 0;

	public void shootAction() {
		shootIndex++;
		if (shootIndex % 30 == 0) {
			Bullet[] bs = hero.shoot();
			bullets = Arrays.copyOf(bullets, bullets.length + bs.length);
			System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
					bs.length);
		}
	}

	/**
	 * 添加定时器
	 */
	private Timer timer;
	private int intervel = 1000 / 100;// 时间间隔

	private void action() {
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				enterAction();
				stepAction();
				
				shootAction();
				
				repaint();// 重绘面板
			}
		}, intervel, intervel);
	}

	public static void main(String[] args) {
		JFrame jFrame = new JFrame("微信打飞机");
		ShootGame game = new ShootGame();
		jFrame.add(game);
		jFrame.setSize(WIDTH, HEIGHT);
		jFrame.setAlwaysOnTop(true);// 始终在最上面
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默认关闭操作
		jFrame.setResizable(false);// 设置窗口大小不可改变
		jFrame.setVisible(true);

		game.action();
	}
}
